<?php

 /*
  * This file is part of the People Of Walmart block for Moodle.
  * Copyright (C) 2009-2010 <anonymousmoodler@gmail.com>
  * 
  * The People Of Walmart block is distributed under the terms of the GNU
  * General Public License as published by the Free Software Foundation, either
  * version 3 of the License, or (at your option) any later version. You should
  * have received a copy of the GNU General Public License along with the People
  * Of Walmart block. If not, see <http://www.gnu.org/licenses/>.
  */

class block_peopleofwalmart extends block_base
{

    function init()
    {
        global $CFG;

        $this->title   = "People Of Walmart";
        $this->version = 2010011400;
        $this->cron    = 300;

        // set some paths
        $this->cache_dir = $CFG->dataroot."/cache/peopleofwalmart";
        $this->cache_html_file = $this->cache_dir."/content.html";

        // these will eventually be on a settings page
        $this->number_of_thumbnails = 4;  // number of images to display (10 maximum)
        $this->enable_thumbnail_cache = FALSE;  // experimental!
    }

    function preferred_width()
    {
        return 200;
    }

    // generates the HTML for the block
    function get_content()
    {
        global $CFG;

        if($this->content !== NULL)
        {
            return $this->content;
        }

        $this->content = new stdClass;
        $this->content->footer = "";

        // if the cache file doesn't exist, run the cron function to create it
        if(!file_exists($this->cache_html_file))
        {
            $this->update_cache();
        }

        $this->content->text = "\n<center>\n";

        // read from the cache file
        $cache_fp = fopen($this->cache_html_file, "r");
        for($i = 0; !feof($cache_fp) && $i < $this->number_of_thumbnails; $i++)
        {
            $this->content->text .= fgets($cache_fp);
        }
        fclose($cache_fp);
        unset($cache_fp);

        $this->content->text .= "</center>\n";

        // set the size of the images based on the column width
        global $preferred_width_left;
        global $preferred_width_right;
        if($this->instance->position == BLOCK_POS_LEFT && !empty($preferred_width_left))
        {
            $imgwidth = $preferred_width_left * 0.8;
        }
        else if($this->instance->position == BLOCK_POS_RIGHT && !empty($preferred_width_right))
        {
            $imgwidth = $preferred_width_right * 0.8;
        }
        else
        {
            $imgwidth = 160;
        }
        $imgheight = $imgwidth * 0.625;
        $this->content->text = str_replace("\$@IMGWIDTH@\$", round($imgwidth), $this->content->text);
        $this->content->text = str_replace("\$@IMGHEIGHT@\$", round($imgheight), $this->content->text);

        return $this->content;
    }

    // updates and caches the content
    function update_cache()
    {
        global $CFG;

        // retrieve RSS feed
        $rss_fp = fopen("http://www.peopleofwalmart.com/?feed=rss2", "r");
        $rss_content = "";
        while(!feof($rss_fp))
        {
            $rss_content .= fread($rss_fp, 8192);
        }
        fclose($rss_fp);
        unset($rss_fp);

        // replace some annoying entities
        $entities = array(8216 => "'", 8217 => "'", 8220 => "&quot;", 8221 => "&quot;", 8230 => "...");
        foreach($entities as $num => $char)
        {
            $rss_content = str_replace("&#".$num.";", $char, $rss_content);
        }
        unset($entities);

        // extract the info we want
        preg_match_all("/<item>.*".
            "<title>([^<>]+)<\/title>.*".
            "<link>(http:\/\/www\.peopleofwalmart\.com\/[a-z0-9?=&%]+)<\/link>.*".
            "<description><!\[CDATA\[(.*)\]\]><\/description>.*".
            "<content:encoded><!\[CDATA\[.*<img[^<>]*(http:\/\/media\.peopleofwalmart\.com\/wp-content\/uploads\/[a-z0-9\/]+\.jpg).*\]\]><\/content:encoded>.*".
            "<\/item>/imsU", $rss_content, $rss_parts, PREG_PATTERN_ORDER);
        unset($rss_content);

        // create the cache directory if necessary
        @mkdir($this->cache_dir);

        // open the cache file for writing and truncate it, or create it if necessary
        $cache_fp = fopen($this->cache_html_file, "w");

        // write the HTML content to the cache file
        for($i = 0; $i < count($rss_parts[0]); $i++)
        {
            $rss_parts[3][$i] = preg_replace("/[\r\n]+/", " —&nbsp;", trim($rss_parts[3][$i], " \r\n/"));
            if($this->enable_thumbnail_cache === TRUE && function_exists("imagejpeg"))
            {
                // thumbnail caching is experimental!
                $rss_parts[4][$i] = $CFG->wwwroot."/blocks/peopleofwalmart/thumbnail.php?img=".urlencode($rss_parts[4][$i])."&w=\$@IMGWIDTH@\$&h=\$@IMGHEIGHT@\$";
            }
            fwrite($cache_fp, 
                "<a target=\"_blank\" href=\"".$rss_parts[2][$i]."\">".
                "<b>".$rss_parts[1][$i]."</b>".
                "<img alt=\"\" title=\"".$rss_parts[3][$i]."\" style=\"".
                "display:block; width:\$@IMGWIDTH@\$px; height:\$@IMGHEIGHT@\$px; margin:2px 0px 15px 0px; border:1px solid #666666;".
                "\" src=\"".$rss_parts[4][$i]."\" /></a>\n");
        }
        fclose($cache_fp);
        unset($cache_fp);
    }

    function cron()
    {
        // we're going to time how long it takes
        $starttime = microtime();

        // update and cache content
        $this->update_cache();

        // output some info
        mtrace("");  // empty line, for cleanliness
        mtrace("People Of Walmart content updated and cached in ".microtime_diff($starttime, microtime())." seconds.");

        return true;
    }

}

?>